<!DOCTYPE html>
<!--
This is a starter template page. Use this page to start your new project from
scratch. This page gets rid of all links and provides the needed markup only.
-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>认证中心登录页面</title>
</head>
<body>
<div style="width:800px;margin:0 auto">
    <input type="text" value="" id="name"/><br/>
    <input type="password" value="" id="password"/><br/>
    <button id="login" onclick="doLogin()">登录</button>
</div>

<script th:src="@{js/main.js}"></script>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
    $(function () {
        checkLoginInLoginPage();
    })

    function checkLoginInLoginPage() {
        var token = localStorage.getItem("token");
        console.log(token)
        if (token == null || token.length == 0) {
            goLogin();
        } else {
            //有token，检查token是否还有效
            $.ajax({
                type: 'get',
                url: "/sso/checkJwt",
                beforeSend: function (request) {      //使用beforeSend
                    request.setRequestHeader("Access-Token", token);
                    request.setRequestHeader("Content-Type", "application/json");
                },
                data: {"token": token},
                success: function (res) {
                    console.log(res);
                    if (res.data === true) {
                        alert('已登录，跳转到回调页面');
                        var redirect = getUrlParam("redirect");
                        console.log(redirect)
                        console.log(redirect != null)
                        if (redirect != null)
                            window.location.href = redirect;
                        else
                            window.location.href = "/sso/index";
                    } else {
                        goLogin();
                    }
                }
            });
        }
    }

    function doLogin() {
        var name = $('#name').val();
        var password = $('#password').val();
        $.ajax({
            type: 'post',
            url: "/sso/login",
            contentType: "application/json",
            dataType: 'json',
            data: JSON.stringify({username: name, password: password}),
            success: function (res) {
                console.log(res)
                if (res.code == 1) {
                    localStorage.setItem("token", res.data);
                    console.log(res)
                    console.log(res.data)
                    alert("登录成功，跳转到回调地址");
                    var redirect = getUrlParam("redirect");
                    if (redirect != null)
                        window.location.href = redirect;
                    else
                        window.location.href = "/sso/index";
                } else {
                    alert("账号密码错误");
                }
            }
        })
    }

    function getToUrl(url, token) {
        $.ajax({
            type: 'get',
            url: "/sso/forwardTo",
            beforeSend: function (request) {      //使用beforeSend
                request.setRequestHeader("Access-Token", token);
                request.setRequestHeader("Content-Type", "application/json");
            },
            data: {"url": url},
            contentType: "application/html",
            success: function (page) {
                window.location.href = "/sso/" + url;
            },
            error: function () {
                alert("跳转失败")
            }
        })
    }
</script>
</body>
</html>